<template>
  <div class="p-4">
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <a-button type="primary" v-auth="['demo:tree:add']" :preIcon="IconEnum.ADD" @click="handleAdd">{{ t('action.create') }}</a-button>
          <a-button type="primary" v-auth="['demo:tree:import']" :preIcon="IconEnum.IMPORT" @click="handleImport">{{ t('action.import') }}</a-button>
          <a-button type="primary" v-auth="['demo:tree:export']" :preIcon="IconEnum.EXPORT" @click="onExportXls">{{ t('action.export') }}</a-button>
        <a-button type="primary" preIcon="ic:round-expand" @click="expandAll">{{ t('action.expandAll') }}</a-button>
        <a-button type="primary" preIcon="ic:round-compress" @click="collapseAll">{{ t('action.collapseAll') }}</a-button>
      </template>
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" />
      </template>
    </BasicTable>
    <TreeModal @register="registerModal" @success="handleSuccess" />
      <JImportModal @register="registerModalJimport" :url="getImportUrl" online />
  </div>
</template>
<script lang="ts" name="demo-tree" setup>
  import {  ref } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n'
  import { IconEnum } from '/@/enums/appEnum'
  import { BasicTable, TableAction } from '/@/components/Table';
  import { useListPage } from '/@/hooks/system/useListPage';
  import { useModal } from '/@/components/Modal';
  import JImportModal from '/@/components/Form/src/jeecg/components/JImportModal.vue';
  import TreeModal from './TreeModal.vue';
  import { columns,searchFormSchema } from './tree.data';
  import { getPage, deleteById, getExportUrl, getImportUrl } from './tree.api';
  const { t } = useI18n();
  const checkedKeys = ref<Array<string | number>>([]);
  const [registerModal, { openModal }] = useModal();
  const [registerModalJimport, { openModal: openModalJimport }] = useModal();
  // 列表页面公共参数、方法
  const { onExportXls, onImportXls, tableContext } = useListPage({
    designScope: 'tree-template',
    tableProps: {
      title: '树表管理列表',
      api: getPage,
      columns: columns,
      size: 'small',
      pagination: false,
      isTreeTable: true,
      striped: true,
      useSearchForm: true,
      showTableSetting: true,
      bordered: true,
      showIndexColumn: false,
      tableSetting: { fullScreen: true },
      formConfig: {
        labelWidth:80,
        autoSubmitOnEnter: true,
        owProps: { gutter: 24 },
        schemas: searchFormSchema,
        autoAdvancedCol: 4,
        baseColProps: { xs: 24, sm: 12, md: 6, lg: 6, xl: 6, xxl: 6 },
        actionColOptions: { xs: 24, sm: 12, md: 6, lg: 6, xl: 6, xxl: 6 },
      },
      actionColumn: {
        width: 180,
      },
    },
    exportConfig: {
      name: '树表管理列表',
      url: getExportUrl,
    },
    importConfig: {
      url: getImportUrl,
    },
  });
  //注册table数据
  const [registerTable, { reload, expandAll, collapseAll }] = tableContext;

  /**
   * 新增
   */
  function handleAdd() {
    openModal(true, {
      isUpdate: false,
    });
  }

  /**
   * 编辑
   */
  function handleEdit(record) {
    openModal(true, {
      record,
      isUpdate: true,
    });
  }

  /**
   * 添加下级
   */
  function handleAddSub(record) {
    openModal(true, {
      record,
      isUpdate: false,
    });
  }
  /**
   * 删除
   */
  async function handleDelete(record) {
    await deleteById({ id: record.id }, reload);
  }

  /**
   * 成功回调
   */
  function handleSuccess() {
    reload();
  }

  /**
   * 操作栏
   */
  function getTableAction(record) {
    return [
      {
        label: t('action.edit'),
        auth:'demo:tree:edit',
        onClick: handleEdit.bind(null, record),
      },
      {
        label: t('action.addSub'),
        auth:'demo:tree:addSub',
        onClick: handleAddSub.bind(null, { parentId: record.id }),
      },
      {
        label: t('action.delete'),
        auth:'demo:tree:delete',
        color: 'error',
        popConfirm: {
          title: t('common.delMessage'),
          confirm: handleDelete.bind(null, record),
        },
      },
    ];
  }

  /**
   * 打开导入弹框
   */
  function handleImport() {
    openModalJimport(true);
  }
</script>

